using System;
using System.Collections.Generic;

using IBatisNet.DataMapper;

using log4net;

using GymManager.Exception;
using GymManager.Persistence.VO;

namespace GymManager.Persistence.DAO
{
    /// <summary>
    /// Class responsible by getting information from database about modality plan
    /// </summary>
    public class ModalityPlanDAO
    {
        // logger instance
        private ILog _logManager = LogManager.GetLogger(typeof(ModalityPlanDAO));

        /// <summary>
        /// Method responsible by getting all modality plans beloging to an specific modality
        /// </summary>
        /// <param name="Int32">modality identifier</param>
        /// <returns>IList - list of modality plans belonging to an specific modality</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public IList<ModalityPlan> findByModality(Int32 modalityId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // modality plan list to be returned
            IList<ModalityPlan> modalityPlanList = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                modalityPlanList = ibatisMapper.QueryForList<ModalityPlan>("ModalityPlan.findByModality", modalityId);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving modality plan information", ex);
                throw new PersistenceException("Error while retrieving modality plan information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return modalityPlanList;
        }

        /// <summary>
        /// Method responsible by finding a modality plan by its composed key 
        /// </summary>
        /// <param name="Int32">frequency (1-5)</param>
        /// <param name="PaymentPlan">payment plan</param>
        /// <param name="Int32">modality identifier</param>
        /// <returns>ModalityPlan - modality plans in according to key</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public ModalityPlan findByKey(Int32 frequency, PaymentPlan paymentPlan, Int32 modalityId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            ModalityPlan modalityPlanVO = null;

            try
            {
                // building the value object to be use as filter
                ModalityPlan modalityPlanFilterVO = new ModalityPlan();
                modalityPlanFilterVO.Frequency = frequency;
                modalityPlanFilterVO.PaymentPlan = paymentPlan;
                modalityPlanFilterVO.ModalityId = modalityId;

                ISqlMapper ibatisMapper = DAOManager.getInstance();
                modalityPlanVO = ibatisMapper.QueryForObject<ModalityPlan>("ModalityPlan.findByKey", modalityPlanFilterVO);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving modality plan information", ex);
                throw new PersistenceException("Error while retrieving modality plan information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return modalityPlanVO;
        }

        /// <summary>
        /// Method responsible by returning the number of students using a given modality plan
        /// </summary>
        /// <param name="Int32">modality plan identifier</param>
        /// <returns>Int32 - number of students using the given modality plan</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public Int32 getNumberOfStudentsUsingModalityPlan(Int32 modalityPlanId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            Int32 numberOfStudentsUsingModalityPlan = 0;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                numberOfStudentsUsingModalityPlan = ibatisMapper.QueryForObject<Int32>("ModalityPlan.getNumberOfStudentsUsingModalityPlan", modalityPlanId);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving modality plan information", ex);
                throw new PersistenceException("Error while retrieving modality plan information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return numberOfStudentsUsingModalityPlan;
        }
    }
}
